﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Teenusekiht ;

public partial class UserControls_CommInControl : System.Web.UI.UserControl
{

    public byte Sets {get;set;}
    private bool IsByEvent = false ;

    private ASP . CommControl cic ;

    private HyperLink link ;
    private Button button ;
    private Button extra ;
    private Label message ;
    private Label label ;
    private DropDownList list ;

    private int? kId ;

    private int? cId = null ;
    private string uName ;
    private string dText ;
    private ushort dValue ;

    private CommServ cs ;
    private GenCom result = null ;
    private RatCom [] values = null ;

    protected void Page_Init ( object sender , EventArgs e )
    {
        cic = ( ASP . CommControl ) LoadControl ( "~/UserControls/CommControl.ascx" ) ;
        divCenter . Controls . Add ( cic ) ;
    }

    protected void Page_Load ( object sender , EventArgs e )
    {
        cs = new CommServ ( Sets ) ;
//ülesse võiks panna lingi, mis kohviku või toidu kommentaariga on tegu...
        //link = new HyperLink () ;
        //divCenter . Controls . Add ( link ) ;
        button = new Button () ;
        divLeft . Controls . Add ( button ) ;
        //extra = new Button () ;
        //divLeft . Controls . Add ( extra ) ;
        message = new Label () ;
        divLeft . Controls . Add ( message ) ;
        label = new Label () ;
        divRight . Controls . Add ( label ) ;
        list = new DropDownList () ;
        divRight . Controls . Add ( list ) ;
        button . Click += new EventHandler ( Button_Click ) ;
        //extra . Click += new EventHandler ( Extra_Click ) ;
        if ( ! IsPostBack ) {
            RetrieveData () ;
            ShowData () ;
            HiddenV . Value = "" ;
            HiddenF . Value = "" ; }
    }

    public void SetVals ( int? kId , int? uId , string uNa , string uDi )
    {
        this . kId = kId ;
        cic . Sets = Sets ;
        cic . IsInp = true ;
        cic . SetVals ( kId , uId , uNa , uDi ) ;
    }

    private void RetrieveData ()
    {
        result = new GenCom () ;
        values = cs . RetrRates () ; //SEELLE SEATUST TULEKS KA KONTROLLIDA!! (sealjuures ka result'i seatust)
    }

    private void ShowData ()
    {
            button . Text = SETS . COMMSENDCOMM ;
            button . Width = Unit . Parse ( "15em" ) ;
            //extra . Text = SETS . COMMSENDCOMM ;
            //extra . Width = Unit . Parse ( "15em" ) ;
            switch ( Sets ) {
              case SETS . CAFECU : case SETS . FOODCU : case SETS . CAFECM : case SETS . FOODCM : case SETS . CAFECA : case SETS . FOODCA :
                if ( values != null ) {
                    foreach ( RatCom val in values ) {
                        list . Items . Add ( new ListItem ( val . rDes , "" + val . rVal ) ) ; }
                    if ( list . Items . Count != 0 ) list . SelectedIndex = 0 ; }
                label . Text = SETS . COMMINTERATE ; break ;
              case SETS . FORUMU : case SETS . FORUMM : case SETS . FORUMA :
                divRight . Controls . Remove ( label ) ;
                divRight . Controls . Remove ( list ) ; break ; default : break ; }
        message . Text = "" ;
        cic . SetFields ( result , values ) ;
    }

    protected void Button_Click ( object sender , EventArgs e )
    {
        //SISESTUSE KONTROLLID LISADA!!!!!!!
        byte parsed ;
        dValue = ( byte . TryParse ( list . SelectedValue , out parsed ) ) ? parsed : SETS . COMMRATENONE ;
        EdiCom data = cic . GetFields () ;
        data . dValue = dValue ;
        data . kId = ( int ) kId ;
        result = cs . SetComm ( data ) ;

        IsByEvent = true ;
    }

            //  case SETS . CAFEC : Response . Redirect ( "~/Kohvik/KohvikuKomm.aspx?id=" + kId ) ; break ;
            //label . Text = SETS . COMMSENDFAIL ; }//kas see tuleks tehe midagi page load'is või clear'iga --TÖÖTAB KA NIIVIISI!!!!!!!!!!!!!!!

    protected void Extra_Click ( object sender , EventArgs e )
    {

        IsByEvent = true ;
    }

    protected void Page_PreRender ( object sender , EventArgs e )
    {
        if ( IsByEvent ) {
            Response . Redirect ( SETS . IDLINKCAFEPAGE + kId ) ;
            /*ShowData () ;*/ }
          else if ( IsPostBack ) {
            //ShowData () ;
            HiddenF . Value = "" ; }
    }

    protected void Page_Unload ( object sender , EventArgs e )
    {
        if ( cs != null ) cs . Dispose () ;
    }

}